<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/16/12
 * Time: 4:24 PM
 * To change this template use File | Settings | File Templates.
 */

jimport('joomla.application.component.controllerform');

class VastControllerHosocanbotheophong extends JControllerForm
{

    /**
     * Class constructor.
     *
     * @param   array  $config  A named array of configuration variables.
     *
     * @since	1.6
     */
    function __construct($config = array())
    {

        parent::__construct($config);
    }

    /**
     * Method to add a new record.
     *
     * @return  mixed  True if the record can be added, a JError object if not.
     *
     * @since   11.1
     */
    public function addCustom($key = null, $urlVar = null)
    {
        // Initialise variables.
        $app = JFactory::getApplication();
        $context = "$this->option.edit.$this->context";
        $cid = JRequest::getVar('phong_id', array(), 'post', 'array');

        // Determine the name of the primary key for the data.
        if (empty($key))
        {
            $key = 'id';
        }

        // To avoid data collisions the urlVar may be different from the primary key.
        if (empty($urlVar))
        {
            $urlVar = $key;
        }
        // Get the previous record id (if any) and the current record id.
        $recordId = (int) (count($cid) ? $cid[0] : JRequest::getInt($urlVar));

        // Access check.
        if (!$this->allowAdd())
        {
            // Set the internal error and also the redirect error.
            $this->setError(JText::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'));
            $this->setMessage($this->getError(), 'error');

            $this->setRedirect(
                JRoute::_(
                    'index.php?option=' . $this->option . '&view=' . $this->view_list
                        . $this->getRedirectToListAppend(), false
                )
            );

            return false;
        }

        // Clear the record edit information from the session.
        $app->setUserState($context . '.data', null);

        // Redirect to the edit screen.
        $this->setRedirect(
            JRoute::_(
                'index.php?option=' . $this->option . '&view=' . $this->view_item
                    . $this->getRedirectToItemAppend(), false
            )
        );

        return true;
    }
    /**
     * Method to cancel an edit.
     *
     * @param   string  $key  The name of the primary key of the URL variable.
     *
     * @return  boolean  True if access level checks pass, false otherwise.
     *
     * @since   11.1
     */
    public function cancel($key = null)
    {
        JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app = JFactory::getApplication();
        $model = $this->getModel();
        $table = $model->getTable();
        $checkin = property_exists($table, 'checked_out');
        $context = "$this->option.edit.$this->context";

        if (empty($key))
        {
            $key = $table->getKeyName();
        }

        $recordId = JRequest::getInt($key);

        // Attempt to check-in the current record.
        if ($recordId)
        {
            // Check we are holding the id in the edit list.
            if (!$this->checkEditId($context, $recordId))
            {
                // Somehow the person just went to the form - we don't allow that.
                $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId));
                $this->setMessage($this->getError(), 'error');

                $this->setRedirect(
                    JRoute::_(
                        'index.php?option=' . $this->option . '&view=' . $this->view_list
                            . $this->getRedirectToListAppend(), false
                    )
                );

                return false;
            }

            if ($checkin)
            {
                if ($model->checkin($recordId) === false)
                {
                    // Check-in failed, go back to the record and display a notice.
                    $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
                    $this->setMessage($this->getError(), 'error');

                    $this->setRedirect(
                        JRoute::_(
                            'index.php?option=' . $this->option . '&view=' . $this->view_item
                                . $this->getRedirectToItemAppend($recordId, $key), false
                        )
                    );

                    return false;
                }
            }
        }

        // Clean the session data and redirect.
        $this->releaseEditId($context, $recordId);
        $app->setUserState($context . '.data', null);

        $this->setRedirect(
            JRoute::_(
                'index.php?option=' . $this->option . '&phong_id=' . JFactory::getApplication()->getUserState('phong_id') . '&view=' . $this->view_list
                    . $this->getRedirectToListAppend(), false
            )
        );

        return true;
    }
}